package chat.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import java.util.concurrent.BlockingQueue;

import chat.user.Message;
import chat.user.User;

public class MultiThread {
	
	private static Queue<Message> InQueue = new LinkedList<Message>();
	private static List<Socket> socket_list = new ArrayList<Socket>();
	private static List<User> user_list = new ArrayList<User>();
	
	public MultiThread() throws IOException{
		
		ServerSocket server = null;
		
		new Thread(new SocketWriter()).start();
		
		new Thread(new SocketProcessor()).start();
		
		GroupProcessor groups = new GroupProcessor();
		
		
		try {
			
			server = new ServerSocket(4444);
		}
		catch (IOException e){
			
			System.out.println("Cannot create server on 4444 port");
		}
		
		try{
			
			while (true){
			
				Socket socket = server.accept();
				
				System.out.println("Client connected");
				
				new ServerThread(socket);
				
				//GroupProcessor.getRoot().addSocket(socket);
				
				//System.out.println("test");
			//	addInSocket(socket);
				
			}
		}
		catch(Exception e){
				
			e.printStackTrace();
		}
		
	}
	
	public static boolean RemoveFromInQueue(Message m){
		
		return InQueue.remove(m);
		
	}
	public static boolean addInMessage(Message m){
		
		return InQueue.add(m);
		
	}
	public static boolean addUser (User u){
		
		return user_list.add(u);
		
	}
	public static Message getInMessage(){
		
		return InQueue.peek();
		
	}
	
	public static boolean RemoveFromInQueue(Socket s){
		
		return socket_list.remove(s);
		
	}
	public static boolean addInSocket(Socket s){
		
		return socket_list.add(s);
		
	}
	public static Socket getSocket(int number){
		
		return socket_list.get(number);
		
	}
	
	public static int getCountInQueue(){
		
		return InQueue.size();
	}
	public static int getCountSocketQueue(){
		
		return socket_list.size();
	}
	
	public static void main (String []args){
		
		try {
			new MultiThread();
		}
		catch(IOException e){
			e.printStackTrace();
			
		}
	}

}
